package com.nullpointer.xchat.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.ImageOptions;
import com.nullpointer.xchat.R;
import com.nullpointer.xchat.ViewImageActivity;
import com.nullpointer.xchat.object.MessageContent;
import com.nullpointer.xchat.object.MessageContent.MsgType;
import com.nullpointer.xchat.utils.Common;
import com.nullpointer.xchat.utils.DataManager;
import com.nullpointer.xchat.utils.Emoticons;
import com.nullpointer.xchat.utils.Utils;

public class ChatPrivateAdapter extends ArrayAdapter<MessageContent> {

	private static class ViewHolder {
		public final LinearLayout rootView;
		public final LinearLayout llLeftChat;
		public final ImageView ivLeftAvatar;
		public final TextView tvLeftMessage;
		public final RelativeLayout rlLeftImage;
		public final ProgressBar pbLeftLoading;
		public final ImageView ivLeftImage;
		public final LinearLayout llRightChat;
		public final TextView tvRightMessage;
		public final RelativeLayout rlRightImage;
		public final ProgressBar pbRightLoading;
		public final ImageView ivRightImage;
		public final ImageView ivRightAvatar;

		private ViewHolder(LinearLayout rootView, LinearLayout llLeftChat,
				ImageView ivLeftAvatar, TextView tvLeftMessage,
				RelativeLayout rlLeftImage, ProgressBar pbLeftLoading,
				ImageView ivLeftImage, LinearLayout llRightChat,
				TextView tvRightMessage, RelativeLayout rlRightImage,
				ProgressBar pbRightLoading, ImageView ivRightImage,
				ImageView ivRightAvatar) {
			this.rootView = rootView;
			this.llLeftChat = llLeftChat;
			this.ivLeftAvatar = ivLeftAvatar;
			this.tvLeftMessage = tvLeftMessage;
			this.rlLeftImage = rlLeftImage;
			this.pbLeftLoading = pbLeftLoading;
			this.ivLeftImage = ivLeftImage;
			this.llRightChat = llRightChat;
			this.tvRightMessage = tvRightMessage;
			this.rlRightImage = rlRightImage;
			this.pbRightLoading = pbRightLoading;
			this.ivRightImage = ivRightImage;
			this.ivRightAvatar = ivRightAvatar;
		}

		public static ViewHolder create(LinearLayout rootView) {
			LinearLayout llLeftChat = (LinearLayout) rootView
					.findViewById(R.id.ll_left_chat);
			ImageView ivLeftAvatar = (ImageView) rootView
					.findViewById(R.id.iv_left_avatar);
			TextView tvLeftMessage = (TextView) rootView
					.findViewById(R.id.tv_left_message);
			RelativeLayout rlLeftImage = (RelativeLayout) rootView
					.findViewById(R.id.rl_left_image);
			ProgressBar pbLeftLoading = (ProgressBar) rootView
					.findViewById(R.id.pb_left_loading);
			ImageView ivLeftImage = (ImageView) rootView
					.findViewById(R.id.iv_left_image);
			LinearLayout llRightChat = (LinearLayout) rootView
					.findViewById(R.id.ll_right_chat);
			TextView tvRightMessage = (TextView) rootView
					.findViewById(R.id.tv_right_message);
			RelativeLayout rlRightImage = (RelativeLayout) rootView
					.findViewById(R.id.rl_right_image);
			ProgressBar pbRightLoading = (ProgressBar) rootView
					.findViewById(R.id.pb_right_loading);
			ImageView ivRightImage = (ImageView) rootView
					.findViewById(R.id.iv_right_image);
			ImageView ivRightAvatar = (ImageView) rootView
					.findViewById(R.id.iv_right_avatar);
			return new ViewHolder(rootView, llLeftChat, ivLeftAvatar,
					tvLeftMessage, rlLeftImage, pbLeftLoading, ivLeftImage,
					llRightChat, tvRightMessage, rlRightImage, pbRightLoading,
					ivRightImage, ivRightAvatar);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder vh;
		if (convertView == null) {
			View view = mInflater.inflate(R.layout.item_lv_chat_private,
					parent, false);
			vh = ViewHolder.create((LinearLayout) view);
			view.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		AQuery aq = new AQuery(convertView);
		MessageContent item = getItem(position);
		Integer userID = item.getFromUserID();
		if (userID.equals(DataManager.getInstance(context).getUserID())) {
			// Right content
			vh.llLeftChat.setVisibility(View.GONE);
			vh.llRightChat.setVisibility(View.VISIBLE);
			setViewContent(item, vh.tvRightMessage, vh.ivRightAvatar,
					vh.ivRightImage, vh.rlRightImage, vh.pbRightLoading, aq,
					vh.llRightChat);
		} else {
			// Left content
			vh.llRightChat.setVisibility(View.GONE);
			vh.llLeftChat.setVisibility(View.VISIBLE);
			setViewContent(item, vh.tvLeftMessage, vh.ivLeftAvatar,
					vh.ivLeftImage, vh.rlLeftImage, vh.pbLeftLoading, aq,
					vh.llLeftChat);
		}
		// prevUserID = item.getFromUserID();
		return vh.rootView;
	}

	private void setViewContent(final MessageContent item, TextView tvMessage,
			ImageView ivAvatar, ImageView ivImage, RelativeLayout rlImage,
			ProgressBar pbProgressBar, AQuery aq, LinearLayout llContent) {

		if (item.getMsgType() == MsgType.IMAGE) {
			tvMessage.setVisibility(View.GONE);
			rlImage.setVisibility(View.VISIBLE);
			if (item.getContent() != null) {
				ImageOptions options = new ImageOptions();
				options.round = 5;
				options.fileCache = true;
				options.memCache = true;
				options.targetWidth = 200;
				// options.fallback =
				aq.id(ivImage).progress(pbProgressBar)
						.image(item.getContent(), options);
				ivImage.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(context,
								ViewImageActivity.class);
						intent.putExtra(Common.ExtraKey.IMAGE_URL,
								item.getContent());
						context.startActivity(intent);
					}
				});
			}
			llContent.setOnLongClickListener(null);
		} else {
			tvMessage.setVisibility(View.VISIBLE);
			rlImage.setVisibility(View.GONE);
			if (item.getContent() != null) {
				tvMessage.setText(Emoticons.getSmiledText(context,
						item.getContent()));
				llContent.setOnLongClickListener(new OnLongClickListener() {

					@Override
					public boolean onLongClick(View v) {
						AlertDialog.Builder aBuilder = new AlertDialog.Builder(
								context);
						aBuilder.setItems(new String[] { "Copy message" },
								new Dialog.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										switch (which) {
										case 0:
											Utils.copyTextToClipboard(context, item
													.getContent());
											break;
										default:
											break;
										}
									}
								});

						AlertDialog alertDialog = aBuilder.create();
						alertDialog.setCanceledOnTouchOutside(true);
						alertDialog.show();
						return false;
					}
				});
			} else {
				tvMessage.setText("");
				llContent.setOnLongClickListener(null);
			}

		}
	}

	private LayoutInflater mInflater;
	private Context context;

	// private Integer prevUserID = null;

	// Constructors
	public ChatPrivateAdapter(Context context, List<MessageContent> objects) {
		super(context, 0, objects);
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

	public ChatPrivateAdapter(Context context, MessageContent[] objects) {
		super(context, 0, objects);
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

}
